﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using Acms.SqlDal;

namespace Acms.Bll
{
    public class Feed : BaseItem
    {
        public int Id { get; private set; }
        public int SiteId { get; private set; }
        public string Name { get; set; }
        public DateTime Created { get; private set; }

        protected Feed() : base() { }

        protected Feed(Hashtable table) : base(table) { }

        protected Feed(Hashtable table, ICollection collection) : base(table, collection) { }

        public static Feed CreateInstance()
        {
            return new Feed();
        }

        protected override Base CreateDal()
        {
            return new FeedDal();
        }

        private static Feed Get(Hashtable table)
        {
            if (table == null)
            {
                return null;
            }
            else
            {
                return new Feed(table);
            }

        }

        private static List<Feed> Get(Hashtable[] tables)
        {
            if (tables == null)
            {
                return null;
            }
            else
            {
                List<Feed> tmp = new List<Feed>();
                foreach (Hashtable table in tables)
                {
                    tmp.Add(new Feed(table, tmp));
                }
                return tmp;
            }
        }

        protected override void Load(Hashtable table)
        {
            Id = (int)table[FeedDal.Fields.ID];
            SiteId = (int)table[FeedDal.Fields.SiteId];
            Name = (string)table[FeedDal.Fields.Name];
            Created = (DateTime)table[FeedDal.Fields.Created];
        }


        public static Feed GetById(int id)
        {
            FeedDal dal = new FeedDal();
            return Get(dal.GetById(id));
        }

        public static Feed GetById(int siteId, string name)
        {
            FeedDal dal = new FeedDal();
            return Get(dal.GetBySiteIdAndName(siteId, name));
        }

        public static List<Feed> GetAllBySiteId(int siteId)
        {
            FeedDal dal = new FeedDal();
            return Get(dal.GetAllBySiteId(siteId));
        }
    }
}
